class TeamController < ApplicationController
  before_filter :sys_before_log

  #获取所有组队的信息
  def getTeams
    total = Team.count
    teams = Team.findPage params[:start].to_i,params[:limit].to_i
    
    jsonStr="{team:"+teams.to_json(:include => {:managers => {:only => :name}})+",total:"+total.to_s+"}"
    render :text => jsonStr,:layout => false
  end

  def getAllTeams
    forCBB = params[:for_cbb]
    if forCBB.nil?
      total = Team.count
      teams = Team.all
    else
      teams = Team.where("is_locked = 0")
      total = teams.length
    end    

    jsonStr="{team:"+teams.to_json()+",total:"+total.to_s+"}"
    render :text => jsonStr,:layout => false
  end

  def create
    begin
      team = Team.find_by_name params[:name]
      if team != nil
        info = '组队已存在，请更换！'
      else
        team = Team.new
        team.name = params[:name]
        team.worker_id = params[:worker_id].to_s
        team.save!
        info = 'success'
      end
    rescue Exception => e
      logger.info e
      info = '创建异常'
    end

    result = "{success:true,info:\"#{info}\"}"
    render :text => result,:layout => false
  end

  #锁定或解锁，只允许锁定。
  def doLock
    ids= params[:id][1..params[:id].length-2].split(',')
    for id in ids
      team = Team.find(id)
      if team.is_locked == 1
        team.is_locked = 0
      else
        team.is_locked = 1
      end
      team.save!
    end
    info = "success"
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

  #指定人员为组队的控制人员
  def assign
    begin
      ids= params[:id][1..params[:id].length-2].split(',')
      for id in ids
        worker = Worker.find_by_id(id)
        worker.is_manager = 1
        worker.save
      end
      info = 'success'
    rescue Exception=>e
      logger.info e
      info = '操作异常'
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #取消人员为组队的控制人员
  def unassign
    begin
      ids= params[:id][1..params[:id].length-2].split(',')
      for id in ids
        worker = Worker.find_by_id(id)
        worker.is_manager = 0
        worker.save
      end
      info = 'success'
    rescue Exception=>e
      logger.info e
      info = '操作异常'
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #获取指定组队的信息
  def edit
    team = Team.find(params[:id])
    jsonStr = team.to_json()
    render :text=>jsonStr,:layout=>false
  end

  def update
    begin
      team = Team.find(params[:id])
      if team.name == params[:name].to_s
        team.worker_id = params[:worker_id].to_s
        team.save
        info = 'success'
      else
        teamName = Team.find_by_name params[:name]
        if teamName == nil
          team.name = params[:name]
          team.worker_id = params[:worker_id].to_s
          team.save
          info = 'success'
        else
          info = '该组队已存在，请更换！'
        end
      end
    rescue Exception=>e
      logger.info e
      info = '更新异常'
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

end
